package utils

import (
	"fmt"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	_ "image/gif"
	"log"
	"math/rand"
	"strconv"
	"time"
)

// GetTurfGrid 获取所处，草坪所在方格
func GetTurfGrid(X, Y float64) (i int, j int) {
	j = int((X - 50) / 80)
	i = int((Y - 75) / 100)

	return i, j
}

// GetRandomNumber 设置随机数
func GetRandomNumber(n int) int {
	return rand.Intn(n)
}

// LoadGrowPlantImage 加载每种植物的连图
func LoadGrowPlantImage() map[string][]*ebiten.Image {

	var PlantPath string
	GrowPlantImage := map[string][]*ebiten.Image{}
	// 豌豆射手
	GrowPlantImage["peashooter"] = []*ebiten.Image{}
	PeaShooterImageSlice := []*ebiten.Image{}

	for i := 0; i < 13; i++ {
		PlantPath = "PlantsVsZombies1/image/Plants/Peashooter/Peashooter/frame_" + strconv.Itoa(i) + ".Png"
		img, _, err := ebitenutil.NewImageFromFile(PlantPath)
		if err != nil {
			log.Fatal(err)
		}
		PeaShooterImageSlice = append(PeaShooterImageSlice, img)
		// fmt.Println(PlantPath)
	}
	GrowPlantImage["peashooter"] = PeaShooterImageSlice
	// 阳光向日葵
	GrowPlantImage["sunFlower"] = []*ebiten.Image{}
	SunFlowerImageSlice := []*ebiten.Image{}

	for i := 0; i < 18; i++ {
		PlantPath = "PlantsVsZombies1/image/Plants/SunFlower/SunFlower/frame_" + strconv.Itoa(i) + ".Png"
		img, _, err := ebitenutil.NewImageFromFile(PlantPath)
		if err != nil {
			log.Fatal(err)
		}
		SunFlowerImageSlice = append(SunFlowerImageSlice, img)
		// fmt.Println(PlantPath)

	}
	GrowPlantImage["sunFlower"] = SunFlowerImageSlice
	// 樱桃炸弹
	GrowPlantImage["cherryBomb"] = []*ebiten.Image{}
	CherryBombImageSlice := []*ebiten.Image{}

	for i := 0; i < 6; i++ {
		PlantPath = "PlantsVsZombies1/image/Plants/CherryBomb/CherryBomb/frame_" + strconv.Itoa(i) + ".Png"
		img, _, err := ebitenutil.NewImageFromFile(PlantPath)
		if err != nil {
			log.Fatal(err)
		}
		CherryBombImageSlice = append(CherryBombImageSlice, img)
	}
	GrowPlantImage["cherryBomb"] = CherryBombImageSlice
	// 小坚果
	GrowPlantImage["nutWall"] = []*ebiten.Image{}
	NutWallImageSlice := []*ebiten.Image{}
	PlantPath = "PlantsVsZombies1/image/Plants/WallNut/WallNut.gif"
	img, _, err := ebitenutil.NewImageFromFile(PlantPath)
	if err != nil {
		log.Fatal(err)
	}
	NutWallImageSlice = append(NutWallImageSlice, img)
	GrowPlantImage["nutWall"] = NutWallImageSlice

	fmt.Println("豌豆射手，向日葵，樱桃炸弹，坚果", "等动态资源加载完毕")
	return GrowPlantImage
}

func CalTimeDiff(t1, t2 time.Time) int { // 用毫秒来计算时间差值
	t3 := t2.Sub(t1)
	return int(t3.Milliseconds())
}

// LoadAdventureEntranceImage 加载MainMenu的图片
func LoadAdventureEntranceImage() (EntranceImageSlice []*ebiten.Image) {
	img, _, err := ebitenutil.NewImageFromFile("PlantsVsZombies1/image/interface/MainMenu.png")
	img1, _, err1 := ebitenutil.NewImageFromFile("PlantsVsZombies1/image/interface/Adventure_0.png")
	img2, _, err2 := ebitenutil.NewImageFromFile("PlantsVsZombies1/image/interface/Adventure_1.png")
	if err != nil && err1 != nil && err2 != nil {
		log.Fatal(err)
	}
	EntranceImageSlice = append(EntranceImageSlice, img, img1, img2)
	fmt.Println("MainMenu图片加载完毕")
	return EntranceImageSlice
}

// LoadZombieImageSlice 加载动态僵尸图片
func LoadZombieImageSlice() (zombieImageSlice []*ebiten.Image) {
	for i := 0; i <= 21; i++ {
		path := "PlantsVsZombies1/image/Zombies/Zombie/Zombie/frame_" + strconv.Itoa(i) + ".Png"
		zombieImageSlice = append(zombieImageSlice, LoadImage(path))
	}
	// 僵尸资源加载完成
	fmt.Println("僵尸资源加载完成")
	return zombieImageSlice
}
func LoadZombieDeadImageSlice() (zombieDeadImageSlice []*ebiten.Image) {

	for i := 0; i < 18; i++ {
		path := "PlantsVsZombies1/image/Zombies/Zombie/ZombieLostHead/frame_" + strconv.Itoa(i) + ".Png"
		zombieDeadImageSlice = append(zombieDeadImageSlice, LoadImage(path))
	}

	for i := 0; i < 10; i++ {
		path := "PlantsVsZombies1/image/Zombies/Zombie/ZombieDie/frame_" + strconv.Itoa(i) + ".Png"
		zombieDeadImageSlice = append(zombieDeadImageSlice, LoadImage(path))
	}

	fmt.Println("僵尸死亡资源加载完毕")
	return zombieDeadImageSlice
}
func LoadCherryBombLastImage() (Image *ebiten.Image) {
	path := "PlantsVsZombies1/image/Plants/CherryBomb/Boom.gif"
	return LoadImage(path)
}
func LoadNutWallImage() (Image []*ebiten.Image) {
	path1 := "PlantsVsZombies1/image/Plants/WallNut/Wallnut_cracked1.gif"
	path2 := "PlantsVsZombies1/image/Plants/WallNut/Wallnut_cracked2.gif"
	Image = append(Image, LoadImage(path1), LoadImage(path2))
	return Image
}

func LoadZombieBombImage() (Image *ebiten.Image) {
	return LoadImage("PlantsVsZombies1/image/Zombies/Zombie/BoomDie1.gif")
}

func LoadZombieEatImage() (zombieEatImage []*ebiten.Image) {
	for i := 0; i <= 20; i++ {
		path := "PlantsVsZombies1/image/Zombies/Zombie/ZombieAttack/frame_" + strconv.Itoa(i) + ".Png"
		zombieEatImage = append(zombieEatImage, LoadImage(path))
	}
	// 僵尸资源加载完成
	fmt.Println("僵尸进攻植物资源加载完成")
	return zombieEatImage
}
func LoadZombieEatDeadImage() (zombieEatDeadImage []*ebiten.Image) {
	for i := 0; i <= 9; i++ {
		path := "PlantsVsZombies1/image/Zombies/Zombie/ZombieDie/frame_" + strconv.Itoa(i) + ".Png"
		zombieEatDeadImage = append(zombieEatDeadImage, LoadImage(path))
	}
	// 僵尸资源加载完成
	fmt.Println("僵尸进攻植物资源加载完成")
	return zombieEatDeadImage
}

func LoadImage(path string) *ebiten.Image {
	img, _, err := ebitenutil.NewImageFromFile(path)
	if err != nil {
		log.Fatal(err)
	}
	return img
}

// LoadPeaImage 豌豆2件套
func LoadPeaImage() (peaImage [2]*ebiten.Image) {
	// 加载完整的豌豆
	peaImage[0] = LoadImage("PlantsVsZombies1/image/Plants/PeaBullet/frame_0.Png")
	// 加载打到僵尸后的图像
	peaImage[1] = LoadImage("PlantsVsZombies1/image/Plants/PeaBulletHit/frame_0.Png")
	fmt.Println("豌豆武器加载完毕")
	return peaImage
}

// GetPlantHP 加载植物血量
func GetPlantHP(PlantName string) (HP int) {
	if PlantName == "nutWall" {
		return 6000 // 30s
	} else if PlantName == "peashooter" {
		return 500 // 2s
	} else if PlantName == "cherryBomb" {
		return 100000 // 100s -- 几乎无敌状态
	} else if PlantName == "sunFlower" {
		return 500 // 1.5s
	} else {
		return 0
	}
}
